﻿//********************************************************************************//
//创建日期：<创建日期，2016-3-16>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：新用户计划输入
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using DevExpress.Web;

namespace Web.Pages.YongShui.JiHuaGL
{
    public partial class XinYongHuJHSR : BasePage
    {

        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int YongHuXX_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["_YongHuXX_ID"]);
            }
            set
            {
                ViewState["_YongHuXX_ID"] = value;
            }
        }

        /// <summary>
        /// 当前操作人站点
        /// </summary>
        private string ZhanDianBH
        {
            get
            {
                return Convert.ToString(ViewState["_ZhanDianBH"]);
            }
            set
            {
                ViewState["_ZhanDianBH"] = value;
            }
        }

        /// <summary>
        /// 水量年
        /// </summary>
        private int ShuiLiangN
        {
            get
            {
                return Convert.ToInt32(ViewState["_ShuiLiangN"]);
            }
            set
            {
                ViewState["_ShuiLiangN"] = value;
            }
        }

        /// <summary>
        /// 水量月
        /// </summary>
        private int ShuiLiangM
        {
            get
            {
                return Convert.ToInt32(ViewState["_ShuiLiangM"]);
            }
            set
            {
                ViewState["_ShuiLiangM"] = value;
            }
        }

        /// <summary>
        /// 传过来用户号
        /// </summary>
        private string yonghuh
        {
            get
            {
                return ViewState["yonghuh"] as string;
            }
            set
            {
                ViewState["yonghuh"] = value;
            }
        }

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtYongHuXX
        {
            get
            {
                return ViewState["dtYongHuXX"] as DataTable;
            }
            set
            {
                ViewState["dtYongHuXX"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>  
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 计划用水
        /// </summary>  
        YS_JH_YUANJIHUA _YS_JH_YUANJIHUA = new YS_JH_YUANJIHUA();
        
        /// <summary>
        /// 计划用水BLL
        /// </summary>  
        YS_JH_YUANJIHUABLL _YS_JH_YUANJIHUABLL = new YS_JH_YUANJIHUABLL();

        /// <summary>
        /// 公司信息
        /// </summary>
        YS_JX_GONGSIXX _YS_JX_GONGSIXX = new YS_JX_GONGSIXX();

        /// <summary>
        /// 公司信息bll
        /// </summary>
        YS_JX_GONGSIXXBLL _YS_JX_GONGSIXXBLL = new YS_JX_GONGSIXXBLL();

        /// <summary>
        /// 局信息
        /// </summary>
        YS_JX_JUXINX _YS_JX_JUXINX = new YS_JX_JUXINX();

        /// <summary>
        /// 局信息bll
        /// </summary>
        YS_JX_JUXINXBLL _YS_JX_JUXINXBLL = new YS_JX_JUXINXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        
       
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.yonghuh = this.Request.QueryString["yonghuh"];
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                LoadData();
                Display();
            }
            this.gvYongHuXX.DataSource = this.dtYongHuXX;
            this.gvYongHuXX.DataBind();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic("1=0", "", null);
            base.Render(sender);
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Add;
            Display();
        }

        /// <summary>
        /// 放弃更改
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            GridViewContentTable.Visible = false;
            this.gvYongHuXX.Enabled = cmdYear.Enabled = cmbBeiXiaYue.Enabled = true;
            this.Status = GridViewStatus.Init;
            Display();
        }

        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.YongHuXX_ID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            Display();
            SetData();
        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData()) 
            { 
                SaveData(); 
            }
        }

        /// <summary>
        /// 保存,确认之后执行保存事件
        /// </summary>
        protected void btnSaveFact_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            return true;
        }

        /// <summary>
        /// 下拉框切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbBeiXiaYue_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 可以输入今、明年计划
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShuiLiangN = Convert.ToInt32(cmdYear.SelectedItem.Value.ToString());
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANMC", "ZHANDIANBH", null, string.Format("USERID={0}", LoginInfo.CurrentUser.UserID)).Tables[0];
            this.ZhanDianBH = dtZhanDian.Rows[0]["VALUE"].ToString() == "00" ? "(00,51,52,53,54,55,56,57,58,59)" : "(" + dtZhanDian.Rows[0]["VALUE"].ToString() + ")";
        }

        
        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            cmdYear.Items.Clear();
            string water_y_m = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            this.ShuiLiangN = Convert.ToInt32(water_y_m.Substring(0, 4));
            this.ShuiLiangM = Convert.ToInt32(water_y_m.Substring(4, 2));
            cmdYear.Items.Add(this.ShuiLiangN.ToString());
            cmdYear.Items.Add((this.ShuiLiangN + 1).ToString());
            cmdYear.SelectedIndex = 0;

            int beixiayu = Convert.ToInt32(this.cmbBeiXiaYue.Value);
            int shikaohy = 1;
            //如果类型为0 则为本月新用户 是考核月为1
            //如果类型为1 则为下月新用户 是考核月为1
            //if (beixiayu == 1)
            //{
            //    shikaohy = 1;
            //}
           // this.dtYongHuXX = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0  and I_LEIXING={0} and S_ZHANDIANBH in {1} and I_SHIKAOHY={2} ", beixiayu, this.ZhanDianBH, shikaohy), null);           
            this.dtYongHuXX = _SpHelper.P_YSJH_XINYONGHJHSR(beixiayu, this.ZhanDianBH, shikaohy, -1).Tables[0];
            gvYongHuXX.DataSource = this.dtYongHuXX;
            gvYongHuXX.DataBind();
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    GridViewContentTable.Visible = false;
                    break;
                case GridViewStatus.Search:
                case GridViewStatus.Update:
                    GridViewContentTable.Visible = true;
                    this.gvYongHuXX.Enabled = cmdYear.Enabled = cmbBeiXiaYue.Enabled = false;
                    break;
               
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private bool SaveData()
        {
            int iYear = this.ShuiLiangN;

            OperateState operateState;
            DataTable dt = _YS_JH_YUANJIHUABLL.SelectYS_JH_YUANJIHUAsDynamic(string.Format("S_YONGHUH='{0}' and I_JLZT=0  and I_Y={1}", txtYongHuH.Text, iYear), null);
            int i_return = 0;
            bool success=false;
            if (dt.Rows.Count > 0)
            {
                //添加
                operateState = OperateState.Add;
                //string UpdateValue = String.Format(@"I_M1={0},I_M2={1},
                //I_M3={2},I_M4={3},I_M5={4},I_M6={5},I_M7={6},I_M8={7},I_M9={8},I_M10={9},I_M11={10},I_M12={11},
                //I_TYPE=4,S_ZHANDIANBH={12}",
                //txtM1.Text, txtM2.Text, txtM3.Text, txtM4.Text, txtM5.Text, txtM6.Text,
                //txtM7.Text, txtM8.Text, txtM9.Text, txtM10.Text, txtM11.Text, txtM12.Text,
                //this.ZhanDianBH.TrimStart('(').TrimEnd(')').Substring(0, 2));
                string UpdateValue = String.Format(@"I_M1={0},I_M2={1}, I_M3={2},I_M4={3},I_M5={4},I_M6={5},
                                                     I_M7={6},I_M8={7},I_M9={8},I_M10={9},I_M11={10},I_M12={11}",
                            txtM1.Text, txtM2.Text, txtM3.Text, txtM4.Text, txtM5.Text, txtM6.Text,
                            txtM7.Text, txtM8.Text, txtM9.Text, txtM10.Text, txtM11.Text, txtM12.Text
                       );
                string WhereClause = string.Format("S_YONGHUH='{0}' AND I_Y={1} ", txtYongHuH.Text, iYear);
                i_return = _YS_JH_YUANJIHUABLL.UpdateYS_JH_YUANJIHUAsDynamic(UpdateValue, WhereClause);
                success = i_return > 0 ? true : false;
            }
            else
            {
                //修改
                operateState = OperateState.Modify;
                DataTable YongHuXX = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("S_YONGHUH='{0}' and  I_JLZT=0", txtYongHuH.Text), null);
                YS_JH_YUANJIHUA _YS = new YS_JH_YUANJIHUA();
                _YS.ID = 0;
                _YS.I_TYPE = 4;
                _YS.S_ZHANDIANBH = this.ZhanDianBH.TrimStart('(').TrimEnd(')').Substring(0, 2); 
                _YS.S_YONGHUH = txtYongHuH.Text;
                _YS.I_Y = iYear;
                _YS.S_GUANLIKBH = YongHuXX.Rows[0]["S_GUANLIKBH"].ToString();
                _YS.S_JUBIANH = YongHuXX.Rows[0]["S_JUBIANH"].ToString();
                _YS.S_GONGSIBH = YongHuXX.Rows[0]["S_GONGSIBH"].ToString();
                _YS.I_M1 = Convert.ToInt32(txtM1.Text); _YS.I_M2 = Convert.ToInt32(txtM2.Text);
                _YS.I_M3 = Convert.ToInt32(txtM3.Text); _YS.I_M4 = Convert.ToInt32(txtM4.Text);
                _YS.I_M5 = Convert.ToInt32(txtM5.Text); _YS.I_M6 = Convert.ToInt32(txtM6.Text);
                _YS.I_M7 = Convert.ToInt32(txtM7.Text); _YS.I_M8 = Convert.ToInt32(txtM8.Text);
                _YS.I_M9 = Convert.ToInt32(txtM9.Text); _YS.I_M10 = Convert.ToInt32(txtM10.Text);
                _YS.I_M11 = Convert.ToInt32(txtM11.Text); _YS.I_M12 = Convert.ToInt32(txtM12.Text);
                _YS.I_JLZT = 0;
                _YS.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                _YS.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                _YS.D_APPLY = BusinessTool.GetDataBaseDateTime();
                _YS.D_KNOWN = BusinessTool.GetDataBaseDateTime();
               success= _YS_JH_YUANJIHUABLL.InsertOrUpdate(_YS, null);
            }
            // this.gvYongHuXX.Enabled = true;
            this.gvYongHuXX.Enabled = cmdYear.Enabled = cmbBeiXiaYue.Enabled = true;
            this.Status = GridViewStatus.Init;
            Display();
            return success;
            
            
            //if (i_return > 0 || success)
            //{
            //    _SYS_LOGBLL.Log<YS_JH_YUANJIHUA>(LoginInfo.CurrentUser, WordsStruct.I_YEWULX.计划水.ToString(), "新用户计划输入", operateState, _YS_JH_YUANJIHUA, null);
            //    PageTools.AlertMsg(this, "保存成功！");
            //    GridViewContentTable.Visible = false;
            //    this.gvYongHuXX.Enabled = true;
            //}
            
        }

        /// <summary>
        /// 实时更改总用水量信息
        /// </summary>
        private void ZongYongSL() 
        {
            int ZYSL = 0;
            ASPxTextBox[] textBoxs = { txtM1, txtM2, txtM3, txtM4, txtM5, txtM6, txtM7, txtM8, txtM9, txtM10, txtM11, txtM12 }; 
            for (int i = 0; i < textBoxs.Length; i++)
            {
                ZYSL += Convert.ToInt32(textBoxs[i].Text);
            }
            txtQuanNianLJ.Text = ZYSL.ToString();
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            DataTable dt = _SpHelper.P_YSJH_XINYONGHJHSR(null, "", null,YongHuXX_ID).Tables[0];
            txtYongHuH.Text = dt.Rows[0]["S_YONGHUH"].ToString();
            txtYongHuM.Text = dt.Rows[0]["S_YONGHUM"].ToString();
            txtJuHao.Text = dt.Rows[0]["S_JUBIANH"].ToString();
            txtJuHao2.Text = dt.Rows[0]["JUMINGC"].ToString();
            txtGongSiM.Text = dt.Rows[0]["S_GONGSIBH"].ToString();
            txtGongSiM2.Text = dt.Rows[0]["GONGSM"].ToString();
            
            //string beixiayu = this.cmbBeiXiaYue.Value.ToString();
            //if (beixiayu == "0")//本月
            //{
            //    if (txtGongSiM.Text != "")
            //    {
            //        DataTable GongSiXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=0", txtGongSiM.Text), null);
            //        txtGongSiM2.Text = GongSiXX.Rows[0][2].ToString();
            //    }
            //    if (txtJuHao.Text != "")
            //    {
            //        DataTable JuXinX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=0", txtJuHao.Text), null);
            //        txtJuHao2.Text = JuXinX.Rows[0][3].ToString();
            //    }
            //}

            //else if (beixiayu == "1")//下月
            //{
            //    if (txtGongSiM.Text != "")
            //    {
            //        DataTable GongSiXX = _YS_JX_GONGSIXXBLL.SelectYS_JX_GONGSIXXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=1", txtGongSiM.Text), null);
            //        txtGongSiM2.Text = GongSiXX.Rows[0][2].ToString();
            //    }
            //    if (txtJuHao.Text != "")
            //    {
            //        DataTable JuXinX = _YS_JX_JUXINXBLL.SelectYS_JX_JUXINXesDynamic(string.Format("s_bianhao='{0}' and i_leixing=1", txtJuHao.Text), null);
            //        txtJuHao2.Text = JuXinX.Rows[0][3].ToString();
            //    }
            //}
            
            //将每一条记录1-12月计划用水量显示到下面的输入框
            DataTable JiHuaYongShui = _YS_JH_YUANJIHUABLL.SelectYS_JH_YUANJIHUAsDynamic(string.Format(" I_JLZT=0 and S_YONGHUH='{0}' and I_Y={1}", txtYongHuH.Text, this.ShuiLiangN), null);

            ASPxTextBox[] textBoxs = { txtM1, txtM2, txtM3, txtM4, txtM5, txtM6, txtM7, txtM8, txtM9, txtM10, txtM11, txtM12 };
            
            if (JiHuaYongShui.Rows.Count > 0)
            {
                txtM1.Text = JiHuaYongShui.Rows[0]["I_M1"].ToString();
                txtM2.Text = JiHuaYongShui.Rows[0]["I_M2"].ToString();
                txtM3.Text = JiHuaYongShui.Rows[0]["I_M3"].ToString();
                txtM4.Text = JiHuaYongShui.Rows[0]["I_M4"].ToString();
                txtM5.Text = JiHuaYongShui.Rows[0]["I_M5"].ToString();
                txtM6.Text = JiHuaYongShui.Rows[0]["I_M6"].ToString();
                txtM7.Text = JiHuaYongShui.Rows[0]["I_M7"].ToString();
                txtM8.Text = JiHuaYongShui.Rows[0]["I_M8"].ToString();
                txtM9.Text = JiHuaYongShui.Rows[0]["I_M9"].ToString();
                txtM10.Text = JiHuaYongShui.Rows[0]["I_M10"].ToString();
                txtM11.Text = JiHuaYongShui.Rows[0]["I_M11"].ToString();
                txtM12.Text = JiHuaYongShui.Rows[0]["I_M12"].ToString();
            }
            else
            {
                foreach (ASPxTextBox item in textBoxs)
                {
                    item.Text = "0";
                }
            }

            //for (int i = 0; i < this.ShuiLiangM - 1; i++)
            //{
            //    textBoxs[i].ClientEnabled = false;
            //}
            string water_y_m = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            for (int i = 0; i < 12; i++)
            {
                textBoxs[i].ClientEnabled = this.ShuiLiangN > Convert.ToInt32(water_y_m.Substring(0, 4)) || i + 1 >= this.ShuiLiangM;
            }


            int ZYSL = 0;
            for (int i = 0; i < textBoxs.Length; i++)
            {
                ZYSL += Convert.ToInt32(textBoxs[i].Text);
            }
            txtQuanNianLJ.Text = ZYSL.ToString();

            //textBoxs[this.ShuiLiangM].Focus();
            for (int i = 0; i < 12; i++)
            {
                if (textBoxs[i].ClientEnabled)
                {
                    textBoxs[i].Focus();
                    break;
                }
            }
        }

        /// <summary>
        /// 正则表达式验证只能输入数字
        /// </summary>
        /// <param name="pendingString"></param>
        /// <returns></returns>

        private bool IsNatural_Number(string str)
        {
            System.Text.RegularExpressions.Regex reg1 = new System.Text.RegularExpressions.Regex(@"^[0-9]*$");
            return reg1.IsMatch(str);
        }

        /// <summary>
        /// 前台回调
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            if (e.Parameter == "save")
            {
                ASPxTextBox[] textBoxs = { txtM1, txtM2, txtM3, txtM4, txtM5, txtM6, txtM7, txtM8, txtM9, txtM10, txtM11, txtM12 };
                for (int i = 0; i < 12; i++)
                {
                    if (textBoxs[i].ClientEnabled && Convert.ToInt32(textBoxs[i].Text) == 0)
                    {
                        cr.IsSuccess = false;
                        cr.IsAlertMsg = true;
                        cr.Msg = "计划不能为0！";
                        e.Result = cr.ToString();
                        textBoxs[i].Focus();
                        return;
                    }
                }

                bool result = SaveData();
                if (result)
                {
                    //_SYS_LOGBLL.Log<YS_JH_YUANJIHUA>(LoginInfo.CurrentUser, WordsStruct.I_YEWULX.计划水.ToString(), "新用户计划输入", operateState, YS_JH_YUANJIHUA, null);
                    //GridViewContentTable.Visible = false;
                    //this.gvYongHuXX.Enabled = true;
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "保存成功！";
                    e.Result = cr.ToString();
                }
                
            }
        }

        /// <summary>
        /// grid刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvYongHuXX_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            this.Status = GridViewStatus.Init;
            this.gvYongHuXX.Enabled = cmdYear.Enabled = cmbBeiXiaYue.Enabled = true;
            Display();
        }





        #endregion
    }
}